﻿using System.Linq;
using System.Web.Mvc;
using Icms.Core;
using Icms.Core.Model;
using Icms.Core.Service;
using Icms.WebUI.Builder;
using Icms.WebUI.Dto;
using Omu.Awesome.Mvc;

namespace Icms.WebUI.Controllers
{
    public class RoomController : Cruder<Room, RoomInput>
    {
        private new readonly IRoomService s;

        public RoomController(IRoomService s, IMapper<Room, RoomInput> v)
            : base(s, v)
        {
            this.s = s;
        }

        public virtual ActionResult Search(string search, int page = 1, int ps = 5)
        {
            var src = s.Where(o => o.Title.StartsWith(search));
            var rows = this.RenderView("rows", src.OrderBy(u => u.Id).Pag(page,ps));

            return Json(new { rows, more = src.Count() > page * ps });
        }

        public override ActionResult Create(RoomInput o)
        {
            if (!ModelState.IsValid)
                return View(v.RemapInput(o));
            return Json(new { Id = s.Create(v.MapToEntity(o), Globals.Court()) });
        }
    }
}